<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\common\model\wechat;

use app\common\model\wechat\tralts\WechatModel;
use hiduoke\services\facade\WechatService as Ws;

class WechatOrder extends WechatBaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'wechat_order';

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [

    ];

    use WechatModel;

    public function Shares()
    {
        return $this->hasMany(WechatShare::class, 'transaction_id', 'transaction_id')->append([]);
    }


    public function getShareAttr($val, $data)
    {
        $res = [];
        $res['count'] = (new WechatShare())->where(['transaction_id' => $data['transaction_id']])->count();//分账单数量
        $res['receivers_count'] = (new WechatShareReceiver())->where(['transaction_id' => $data['transaction_id'], 'type' => 'PERSONAL_OPENID'])->count();//参数分账用户数
        return $res;
    }

//    /**
//     * 获取分账信息
//     */
//    public function getShareAttr($val, $data)
//    {
//        $User = new WechatUser();
//        if ($data['is_share'] == 1) {
//            $res = Ws::shareQuery($data['transaction_id'], $data['out_trade_no']);
//            if ($res['return_code'] == "SUCCESS" && $res['result_code'] == "SUCCESS") {
//                $receivers = json_decode($res['receivers'], true);
//                foreach ($receivers as $key => $value) {
//                    if ($value['type'] == "PERSONAL_OPENID") {
//                        $receivers[$key]["user"] = $User->field(['nickname,avatar,id'])->where(['openid' => $value['account']])->find();
//                    }
//                }
//                $res['receivers'] = $receivers;
//                $res['receivers_count'] = count($receivers);
//            }
//            unset($res['sign']);
//            unset($res['mch_id']);
//            unset($res['nonce_str']);
//            return $res;
//        } else {
//            return false;
//        }
//    }
}
